// pages/water/band/index.js
var app = getApp()
var common_Js = require("../../../component/common.js"), fetchApi = require("../../../utils/httpPost.js")
import Toast from '../../../component/vant/toast/toast';
const CryptoJS = require("../../../utils/aesUtil.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    card_num:'',
    submitState: false,
    cardInfo:{},
    phoneNum:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('waterBand ', options);
    if (options.q) {
        var q = decodeURIComponent(options.q) // 获取到二维码原始链接内容
        q = "?" + q.split("?")[1];
        this.data.query = q;
        console.log('q', q)
        options = common_Js.getQueryVariable(q);
    }
    if(options.no){
      this.data.card_num = options.no;
      this.setData({
        card_num: options.no
      })
    }
    
    if(options.n){
      this.data.card_num = options.n;
      this.setData({
        card_num: options.n
      })
    }
    if(this.data.card_num!=''){
      this.onHttpInit();
    }
    var phone_num = app.getStorageSync("phone_num");
    if(phone_num != '' && phone_num != undefined){
      this.data.phoneNum = phone_num;
      this.setData({
        phoneNum: phone_num
      })
    }
  },
  inputHandle(e){
    console.log('inputHandle', e)
    this.data.card_num = e.detail;
  },
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost('/device/water/card/detail', {
      card_num: that.data.card_num,
    }, function (res) {
      console.log("获取二维码数据", res)
      if (res.data.code == 0) {
        that.setData({
          cardInfo: res.data.data.waterCard
        })        
      }else{
        Toast.fail(res.data.msg);
      }
      that.setData({ netWork: true, isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false, isLoading: false, })
    })
  },
  submitOp(){
    if(this.data.card_num == ''){
      Toast('请输入卡号')
      return;
    }
    this.data.submitState = true;
    this.setData({
      submitState : true,
    })
    var that = this;
    fetchApi.httpPost('/device/water/band/card', {
      card_num: that.data.card_num,
      phone_num: that.data.phoneNum,
    }, function (res) {
      if (res.data.code == 0) {
        console.log("绑定水卡返回数据", res.data.data)
        Toast.success(res.data.msg);
        setTimeout(function(){
          wx.navigateBack({
            delta: -1
          })
        }, 300)
      } else {
        console.log("绑定水卡返回数据 异常", res.data.msg)
        Toast.fail(res.data.msg);
      }
      
      that.data.submitState = true;
      that.setData({
        submitState : true,
      })
    }, function () {
      that.setData({ isLoading: false, loading: true, netWork: true, })
    })

  }
})